//
//  UIViewController+Init.m
//  KCGLive
//
//  Created by 李光耀 on 2019/7/19.
//  Copyright © 2019 Roy. All rights reserved.
//

#import "UIViewController+Init.h"
#import <objc/runtime.h>

@implementation UIViewController (Init)

+ (void)load
{
    yb_swizzleMethod([self class], @selector(viewWillAppear:), @selector(yb_viewWillAppear:));
    yb_swizzleMethod([self class], @selector(viewWillDisappear:), @selector(yb_viewWillDisappear:));
}

- (void)yb_viewWillAppear:(BOOL)animated {
    
    
    [self yb_viewWillAppear:animated];
    
}

- (void)yb_viewWillDisappear:(BOOL)animated {
    
    NSDictionary *dict = @{ NSForegroundColorAttributeName : [UIColor blackColor] };
    [self.navigationController.navigationBar setTitleTextAttributes:dict];
    [self yb_viewWillDisappear:animated];
}

//- (void)swizzled_viewWillAppear:(BOOL)animated
//{
//    // call original implementation
//    [self swizzled_viewWillAppear:animated];
//    [YBUserOperationManager isLogined];
//}



void yb_swizzleMethod(Class class, SEL originalSelector, SEL swizzledSelector)
{
    // the method might not exist in the class, but in its superclass
    //    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    //    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    //
    //    // class_addMethod will fail if original method already exists
    //    //假如本类没有实现该原始方法,上面得到的是父类中的实现,因此addMethod会返回yes
    //    BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    //
    //    // the method doesn’t exist and we just added one
    //    if (didAddMethod) {
    //        class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    //    }
    //    else {
    //        method_exchangeImplementations(originalMethod, swizzledMethod);
    //    }
    
    
    Method viewWillAppear = class_getInstanceMethod(class, originalSelector);
    Method new_viewWillAppear = class_getInstanceMethod(class, swizzledSelector);
    method_exchangeImplementations(viewWillAppear, new_viewWillAppear);
}



@end
